package com.leaf.u_spring.chapter12;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import com.mongodb.MongoClient;

/**
 * 
 * Spring Data MongoDB的配置
 *
 */
@Configuration
@EnableMongoRepositories(basePackages="com.adagio")	//启用MongoDB的Repository功能
public class MongoConfig {

	/**
	 * MongoTemplate Bean
	 * @param mongoDbFactory
	 * @return
	 */
	@Bean
	public MongoOperations mongoTemplate(){
		return new MongoTemplate(mongoDbFactory());
	}
	
	/**
	 * MongoDbFactory bean
	 * @return
	 */
	public MongoDbFactory mongoDbFactory(){
		return new SimpleMongoDbFactory(mongoClient(), "com.adagio");
	}
	
	/**
	 * MongoClient bean
	 * @return
	 */
	public MongoClient mongoClient(){
		return new MongoClient("localhost");
	}
	
}
